package com.jxtti.controller;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.github.pagehelper.PageInfo;
import com.jxtti.entity.AdvisoryMsg;
import com.jxtti.entity.ArticleCategory;
import com.jxtti.entity.ArticleList;
import com.jxtti.entity.DeptsIntroduce;
import com.jxtti.entity.DisciplinaryReport;
import com.jxtti.entity.SiteAd;
import com.jxtti.entity.SiteContent;
import com.jxtti.entity.SiteLink;
import com.jxtti.entity.SiteMenu;
import com.jxtti.entity.SolicitPublic;
import com.jxtti.entity.SolicitPublicComment;
import com.jxtti.entity.TDocDocumentsrelease;
import com.jxtti.entity.TWaterSiteHourSrc;
import com.jxtti.service.AdvisoryMsgService;
import com.jxtti.service.ArticleCategoryService;
import com.jxtti.service.ArticleListService;
import com.jxtti.service.DeptsIntroduceService;
import com.jxtti.service.DisciplinaryReportService;
import com.jxtti.service.SiteAdService;
import com.jxtti.service.SiteContentService;
import com.jxtti.service.SiteLinkService;
import com.jxtti.service.SiteMenuService;
import com.jxtti.service.SolicitPublicCommentService;
import com.jxtti.service.SolicitPublicService;
import com.jxtti.service.TDocDocumentsreleaseService;
import com.jxtti.utils.BaseCodec;
import com.jxtti.utils.ExceptionUtil;

/**
 * 网站前台 首页
 *
 * @author bff
 */

@Controller
@RequestMapping("/SiteIndex")
public class WebSiteIndexController {
    private final static Logger LOGGER = Logger.getLogger(WebSiteIndexController.class);
    @Autowired
    private TDocDocumentsreleaseService tDocDocumentsreleaseService;


    @InitBinder  //初始化数据的时候，进行数据类型转换，String类型转为Date类型
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    @Autowired
    private SiteAdService siteAdService;

    //查找广告
    @RequestMapping(value = "/querySiteAd")
    @ResponseBody
    public List<SiteAd> querySiteAd(@RequestParam Map<String, Object> data,
                                    HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<SiteAd> site_ad = siteAdService.queryByList(data);
        return site_ad;
    }

    @Autowired
    private SiteMenuService siteMenuService;

    //首页导航
    @RequestMapping(value = "/querySiteNav")
    @ResponseBody
    public List<SiteMenu> querySiteNav(@RequestParam Map<String, Object> data,
                                       HttpServletRequest request, HttpServletResponse response) throws Exception {
        data.put("is_deleted", "0");
        List<SiteMenu> siteMenu = siteMenuService.queryAll(data);
        return siteMenu;
    }

    @Autowired
    private ArticleListService articleListService;

    //查找文章
    @RequestMapping("/queryArticle")
    @ResponseBody
    public List<ArticleList> queryArticle(@RequestParam Map<String, Object> data,
                                          HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<ArticleList> articleList;
        data.put("is_deleted", "0");
        data.put("status", "2");//状态 草稿0,待审1,审核通过2,审核失败3
        data.put("articleSite", "'0','2'");//文章所属站点 0 外网,1内网，2内外网
        data.put("issue", "1");//是否网页显示
        // data.put("isHomePic", "1");//是否首页图片新闻
        //data.put("isRecommend", "0");//是否列表推荐 预留字段
        articleList = articleListService.queryListForIndex(data);
        //}

        return articleList;
    }


    /***
     * TsysFilesInfo  同步公文信息.
     * --获取所有已发布的公文表单及正文，其中bt是标题；fbrq为发布日期(1为发布，0为未发布)；id为公文ID，主键；filepath 为正文路径;jszt为接收状态，0为未同步，1为同步
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public List<ArticleList> queryTsysFilesInfo(HttpServletRequest request, HttpServletResponse response, Map<String, Object> dValue) {
        Map<String, Object> data = new HashMap();
        // data.put("fbrq", "1");
        data.put("jszt", "1");
        List<TDocDocumentsrelease> lstTDocDocumentsrelease = tDocDocumentsreleaseService.queryAll(data);

        List<ArticleList> lst = new ArrayList();
        for (TDocDocumentsrelease tem : lstTDocDocumentsrelease) {
            ArticleList instance = new ArticleList();
            instance.setDocDocument_id(tem.getId());
            instance.setTitle(tem.getBt());
            instance.setCategoryID(dValue.get("categoryID").toString());
            instance.setContents(tem.getZw());
            lst.add(instance);
        }
        return lst;
    }

    //查找单独一篇文章
    @RequestMapping("/queryArticleById")
    @ResponseBody
    public ArticleList queryArticleById(@RequestParam Map<String, Object> data, ArticleList entity,
                                        HttpServletRequest request, HttpServletResponse response) throws Exception {
        ArticleList art = new ArticleList();
        art.setId(Integer.parseInt(data.get("id").toString()));
        ArticleList articleList = articleListService.queryById(art);
        return articleList;
    }

    @Autowired
    private SiteLinkService siteLinkService;

    //友情链接
    @RequestMapping(value = "/querySiteLink")
    @ResponseBody
    public List<SiteLink> querySiteLink(@RequestParam Map<String, Object> data,
                                        HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<SiteLink> lst = this.siteLinkService.queryAll(data);
        return lst;
    }

    //文章 article.htm
    @RequestMapping(value = "/article")
    @ResponseBody
    public ModelAndView ArticleInterface(@RequestParam Map<String, Object> data,
                                         HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView();
        try {
            String artid = new String(BaseCodec.decode(data.get("id").toString()));//解密
            ArticleList art = new ArticleList();
            art.setId(Integer.parseInt(artid));
            ArticleList article = articleListService.queryById(art);//查找文章
            data.put("article", article);

            if (data.get("preview") == null) { //preview 预览
                //点击数 +1
                article.setClicks(article.getClicks() + 1);
                articleListService.update(article);
            }

            //文章所属的类目 的上级 上级 ....
            Map<String, Object> artCategory = new HashMap<String, Object>();
            artCategory.put("id", article.getCategoryID());
            List<ArticleCategory> artCategoryList = articleCategoryService.findPidAndPid(artCategory);
            data.put("artCategoryList", artCategoryList);

            //文章的上一篇 下一篇
            Map<String, Object> preData = new HashMap<String, Object>();
            preData.put("is_deleted", "0");
            preData.put("status", "2");//状态 草稿0,待审1,审核通过2,审核失败3
            preData.put("articleSite", "'0','2'");//文章所属站点 0 外网,1内网，2内外网
            preData.put("issue", "1");//是否网页显示
            preData.put("midd", artid);//中间 id
            preData.put("categoryID", article.getCategoryID());
            List<ArticleList> preArticle = articleListService.queryPreNext(preData);//查找文章
            data.put("preArticle", preArticle);

            mav.addObject("data", data);
            mav.setViewName("/pages/WebSitePc/ArticleContent.jsp");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mav;
    }

    @Autowired
    private ArticleCategoryService articleCategoryService;

    @Autowired
    private DeptsIntroduceService deptsIntroduceService;

    //文章 类目.htm
    @RequestMapping(value = "/category")
    @ResponseBody
    public ModelAndView CategoryInterface(@RequestParam Map<String, Object> data,
                                          HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView();
        try {
            String catid = new String(BaseCodec.decode(data.get("id").toString()));//解密

            //该类目的上级 上级 ....
            Map<String, Object> artCategory = new HashMap<String, Object>();
            artCategory.put("id", catid);
            List<ArticleCategory> artCategoryList = articleCategoryService.findPidAndPid(artCategory);
            data.put("artCategoryList", artCategoryList);
            //该类目是否有部门--部门简介
            if (!artCategoryList.isEmpty()) {
                int level = artCategoryList.get(artCategoryList.size()).getLevelID();
                java.lang.Integer orgId = artCategoryList.get(artCategoryList.size() - 1).getFk_orgId();
                if (level == 2 && orgId != null) {
                    //查找部门简介
                    Map<String, Object> dataDep = new HashMap<String, Object>();
                    dataDep.put("fk_orgId", orgId);
                    dataDep.put("visible", 1);//显示
                    List<DeptsIntroduce> deptsIntroduce = deptsIntroduceService.queryAll(dataDep);
                    data.put("deptsIntroduce", deptsIntroduce);
                }
            }
            mav.addObject("data", data);
            mav.setViewName("/pages/WebSitePc/Category.jsp");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mav;
    }

    //文章类目树
    @RequestMapping(value = "/getEasyUiTree")
    @ResponseBody
    public List<Map<String, Object>> getEasyUiTree(
            @RequestParam Map<String, String> data, HttpServletRequest request) {
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        try {
            //判断是否超级管理员
            if (null == data.get("parentID")) { //超级管理员不加权限
                data.put("parentID", "0");
            }
            data.put("visible", "1");
            result = articleCategoryService.getEasyUiTree(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    //查找文章--分页
    @RequestMapping("/queryArticlePage")
    @ResponseBody
    public Map<String, Object> queryArticlePag(@RequestParam Map<String, Object> data,
                                               HttpServletRequest request, HttpServletResponse response) throws Exception {
        data.put("is_deleted", "0");
        data.put("status", "2");//状态 草稿0,待审1,审核通过2,审核失败3
        data.put("articleSite", "'0','2'");//文章所属站点 0 外网,1内网，2内外网
        data.put("issue", "1");//是否网页显示

        Map<String, Object> result = new HashMap<String, Object>();
        PageInfo<ArticleList> page = articleListService.listPage(data);
        result.put("total", page.getTotal());
        result.put("rows", page.getList());
        return result;
    }

    @Autowired
    private SiteContentService siteContentService;

    //模块 content.htm
    @RequestMapping(value = "/content")
    @ResponseBody
    public ModelAndView ContentInterface(@RequestParam Map<String, Object> data,
                                         HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView();
        try {
            SiteContent con = new SiteContent();
            SiteContent content = new SiteContent();
            if (data.get("id") != null) {
                String artid = new String(BaseCodec.decode(data.get("id").toString()));//解密id
                con.setId(Integer.parseInt(artid));
                content = siteContentService.queryById(con);//查找模
            } else if (data.get("fk_menuId") != null) {
                String fk_menuId = new String(BaseCodec.decode(data.get("fk_menuId").toString()));//解密
                con.setFk_menuId(Integer.parseInt(fk_menuId));
                content = siteContentService.queryByMenuId(con);// 通过fk_menuId 内容导航
            }
            data.put("content", content);
            mav.addObject("data", data);
            mav.setViewName("/pages/WebSitePc/ModuleContent.jsp");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mav;
    }

    //查找模块
    @RequestMapping(value = "/queryContent")
    @ResponseBody
    public List<SiteContent> queryContent(@RequestParam Map<String, Object> data,
                                          HttpServletRequest request, HttpServletResponse response) throws Exception {
        data.put("issue", "1");
        data.put("is_deleted", "0");
        List<SiteContent> lst = this.siteContentService.queryAll(data);
        return lst;
    }


    //科室介绍
    @RequestMapping(value = "/depart")
    @ResponseBody
    public ModelAndView DepartInterface(@RequestParam Map<String, Object> data,
                                        HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView();
        try {
            DeptsIntroduce depts = new DeptsIntroduce();
            if (data.get("id") != null) {
                //String id=new String(BaseCodec.decode(data.get("id").toString()));//解密id
                String id = data.get("id").toString();//解密id
                depts.setId(Integer.parseInt(id));
                depts = deptsIntroduceService.queryById(depts);
            }
            data.put("deptsIntroduce", depts);
            mav.addObject("data", data);
            mav.setViewName("/pages/WebSitePc/DepartContent.jsp");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mav;
    }

    @Autowired
    private AdvisoryMsgService advisoryMsgService;

    //公众互动--咨询留言--分页- 查询页面
    @RequestMapping(value = "/adviceList")
    @ResponseBody
    public ModelAndView adviceListInterface(@RequestParam Map<String, Object> data,
                                            HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView();
        try {
            //	String muid=new String(BaseCodec.decode(data.get("menuId").toString()));//解密-获取菜单id
            String muid = data.get("muid").toString();
            //该 导航菜单 的上级 上级 ....
            Map<String, Object> siteMenu = new HashMap<String, Object>();
            siteMenu.put("id", muid);
            List<SiteMenu> siteMenuList = siteMenuService.findPidAndPid(siteMenu);
            data.put("siteMenuList", siteMenuList);
            mav.addObject("data", data);
            mav.setViewName("/pages/WebSitePc/PublicInteraction/adviceList.jsp");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mav;
    }

    //查找 咨询留言--分页
    @RequestMapping("/queryAdvicePage")
    @ResponseBody
    public Map<String, Object> queryAdvicePage(@RequestParam Map<String, Object> data,
                                               HttpServletRequest request, HttpServletResponse response) throws Exception {
        data.put("is_deleted", "0");
        if (data.get("searchId") == null || data.get("searchId").equals("")) { //用查询号查询的 不关心是否公开 状态
            data.put("status", "2");//状态  0待回复 1已分配  2已回复  -1垃圾留言
            data.put("is_publish_ad", "1");//是否网页公开
        }
        Map<String, Object> result = new HashMap<String, Object>();
        PageInfo<AdvisoryMsg> page = advisoryMsgService.listPage(data);
        result.put("total", page.getTotal());
        result.put("rows", page.getList());
        return result;
    }


    //公众互动--咨询留言
    @RequestMapping(value = "/adviceAddInterface")
    @ResponseBody
    public ModelAndView adviceAddInterface(@RequestParam Map<String, Object> data,
                                           HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView();
        try {
            String muid = data.get("muid").toString();
            //该 导航菜单 的上级 上级 ....
            Map<String, Object> siteMenu = new HashMap<String, Object>();
            siteMenu.put("id", muid);
            List<SiteMenu> siteMenuList = siteMenuService.findPidAndPid(siteMenu);
            data.put("siteMenuList", siteMenuList);
            mav.addObject("data", data);
            mav.setViewName("/pages/WebSitePc/PublicInteraction/adviceAdd.jsp");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mav;
    }

    //公众互动--咨询留言
    @RequestMapping(value = "/adviceAdd")
    @ResponseBody
    public Map<String, Object> adviceAdd(@RequestParam Map<String, Object> data,
                                         AdvisoryMsg advisoryMsg, HttpSession session, HttpServletRequest request,
                                         HttpServletResponse response) throws Exception {

        ModelAndView mav = new ModelAndView();
        try {
            if (!(data.get("randCode").toString().equalsIgnoreCase(session.getAttribute("code").toString()))) {  //忽略验证码大小写
                data.put("success", false);
                data.put("error", "验证码错误！");
            } else {
                Date now = new Date();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");//可以方便地修改日期格
                String hehe = dateFormat.format(now);
                String searchId = hehe + (int) ((Math.random() * 9 + 1) * 10);
                advisoryMsg.setSearchId(searchId);
                advisoryMsg.setStates(0);
                int exeCount = advisoryMsgService.add(advisoryMsg);
                if (advisoryMsg.getTypeId() != null && advisoryMsg.getTypeId() == 1) {
                    //污染投诉到信访系统
                    advisoryMsgService.addPetition(advisoryMsg);
                }
                if (exeCount > 0) {
                    data.put("success", true);
                    data.put("searchId", searchId);
                } else {
                    data.put("success", false);
                }
            }

        } catch (Exception e) {
            data.put("success", false);
            data.put("error", ExceptionUtil.createStackTrackMessage(e));
            e.printStackTrace();
        }
        return data;
    }

    //公众互动--咨询留言
    @RequestMapping(value = "/adviceView")
    @ResponseBody
    public ModelAndView adviceView(@RequestParam Map<String, Object> data,
                                   HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView();
        try {
            if (data.get("muid") != null) {
                String muid = data.get("muid").toString();
                //该 导航菜单 的上级 上级 ....
                Map<String, Object> siteMenu = new HashMap<String, Object>();
                siteMenu.put("id", muid);
                List<SiteMenu> siteMenuList = siteMenuService.findPidAndPid(siteMenu);
                data.put("siteMenuList", siteMenuList);
            } else {
                data.put("siteMenuList", null);
            }
            // 通过id 查找该留言
            String id = new String(BaseCodec.decode(data.get("id").toString()));//解密-获取留言id
            data.put("id", id);
            List<AdvisoryMsg> advisoryMsg = advisoryMsgService.queryForIndex(data);
            if (advisoryMsg != null) {
                data.put("advisoryMsg", advisoryMsg.get(0));
            }
            mav.addObject("data", data);
            mav.setViewName("/pages/WebSitePc/PublicInteraction/adviceView.jsp");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mav;
    }


    @Autowired
    private DisciplinaryReportService disciplinaryReportService;

    //公众互动--纪检举报
    @RequestMapping(value = "/disciplinaryAddInterface")
    @ResponseBody
    public ModelAndView disciplinaryAddInterface(@RequestParam Map<String, Object> data,
                                                 HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView();
        try {
            String muid = data.get("muid").toString();
            //该 导航菜单 的上级 上级 ....
            Map<String, Object> siteMenu = new HashMap<String, Object>();
            siteMenu.put("id", muid);
            List<SiteMenu> siteMenuList = siteMenuService.findPidAndPid(siteMenu);
            data.put("siteMenuList", siteMenuList);
            mav.addObject("data", data);
            mav.setViewName("/pages/WebSitePc/PublicInteraction/disciplinaryAdd.jsp");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mav;
    }

    //公众互动--纪检举报--分页- 查询页面
    @RequestMapping(value = "/disciplinaryList")
    @ResponseBody
    public ModelAndView disciplinaryListInterface(@RequestParam Map<String, Object> data,
                                                  HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView();
        try {
            //	String muid=new String(BaseCodec.decode(data.get("menuId").toString()));//解密-获取菜单id
            String muid = data.get("muid").toString();
            //该 导航菜单 的上级 上级 ....
            Map<String, Object> siteMenu = new HashMap<String, Object>();
            siteMenu.put("id", muid);
            List<SiteMenu> siteMenuList = siteMenuService.findPidAndPid(siteMenu);
            data.put("siteMenuList", siteMenuList);
            mav.addObject("data", data);
            mav.setViewName("/pages/WebSitePc/PublicInteraction/disciplinaryList.jsp");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mav;
    }

    //公众互动--咨询留言
    @RequestMapping(value = "/disciplinaryView")
    @ResponseBody
    public ModelAndView disciplinaryView(@RequestParam Map<String, Object> data,
                                         HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView();
        try {
            if (data.get("muid") != null) {
                String muid = data.get("muid").toString();
                //该 导航菜单 的上级 上级 ....
                Map<String, Object> siteMenu = new HashMap<String, Object>();
                siteMenu.put("id", muid);
                List<SiteMenu> siteMenuList = siteMenuService.findPidAndPid(siteMenu);
                data.put("siteMenuList", siteMenuList);
            } else {
                data.put("siteMenuList", null);
            }
            // 通过id 查找该留言
            String id = new String(BaseCodec.decode(data.get("id").toString()));//解密-获取留言id
            data.put("id", id);
            List<DisciplinaryReport> disciplinary = disciplinaryReportService.queryForIndex(data);
            if (disciplinary != null) {
                data.put("disciplinary", disciplinary.get(0));
            }
            mav.addObject("data", data);
            mav.setViewName("/pages/WebSitePc/PublicInteraction/disciplinaryView.jsp");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mav;
    }


    //公众互动--咨询留言
    @RequestMapping(value = "/disciplinaryAdd")
    @ResponseBody
    public Map<String, Object> disciplinaryAdd(@RequestParam Map<String, Object> data,
                                               DisciplinaryReport disciplinaryReport, HttpSession session, HttpServletRequest request,
                                               HttpServletResponse response) throws Exception {

        ModelAndView mav = new ModelAndView();
        try {
            if (!(data.get("randCode").toString().equalsIgnoreCase(session.getAttribute("code").toString()))) {  //忽略验证码大小写
                data.put("success", false);
                data.put("error", "验证码错误！");
            } else {
                Date now = new Date();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");//可以方便地修改日期格
                String hehe = dateFormat.format(now);
                String searchId = hehe + (int) ((Math.random() * 9 + 1) * 10);
                disciplinaryReport.setSearchId(searchId);
                disciplinaryReport.setStates(0);
                String ip = getIpAddr(request);
                disciplinaryReport.setCreateIP(ip);
                int exeCount = disciplinaryReportService.add(disciplinaryReport);
                if (exeCount > 0) {
                    data.put("success", true);
                    data.put("searchId", searchId);
                } else {
                    data.put("success", false);
                }
            }

        } catch (Exception e) {
            data.put("success", false);
            data.put("error", ExceptionUtil.createStackTrackMessage(e));
            e.printStackTrace();
        }
        return data;
    }

    //查找 纪检留言--分页
    @RequestMapping("/queryDisciplinaryPage")
    @ResponseBody
    public Map<String, Object> queryDisciplinaryPage(@RequestParam Map<String, Object> data,
                                                     HttpServletRequest request, HttpServletResponse response) throws Exception {
        data.put("is_deleted", "0");
        if (data.get("searchId") == null || data.get("searchId").equals("")) { //用查询号查询的 不关心是否公开 状态
            data.put("states", "2");//状态  0待回复 1已分配  2已回复  -1垃圾留言
            data.put("is_publish_ad", "1");//是否网页公开
        }
        Map<String, Object> result = new HashMap<String, Object>();
        PageInfo<DisciplinaryReport> page = disciplinaryReportService.listPage(data);
        result.put("total", page.getTotal());
        result.put("rows", page.getList());
        return result;
    }

    //查找菜单树
    @RequestMapping(value = "/getMenuTree")
    @ResponseBody
    public List<Map<String, Object>> getMenuTree(
            @RequestParam Map<String, String> data, HttpServletRequest request) {
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        try {
            if (null == data.get("parentID")) {
                data.put("parentID", "0");
            }
            result = siteMenuService.getEasyUiTree(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    //公众互动--咨询留言
    @RequestMapping(value = "/searchInterface")
    @ResponseBody
    public ModelAndView searchInterface(@RequestParam Map<String, Object> data,
                                        HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView();
        try {
            mav.addObject("data", data);
            mav.setViewName("/pages/WebSitePc/searchnw.jsp");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mav;
    }


    //公众互动--民意征集--分页- 查询页面
    @RequestMapping(value = "/solicitList")
    @ResponseBody
    public ModelAndView solicitListInterface(@RequestParam Map<String, Object> data,
                                             HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView();
        try {
            //	String muid=new String(BaseCodec.decode(data.get("menuId").toString()));//解密-获取菜单id
            String muid = data.get("muid").toString();
            //该 导航菜单 的上级 上级 ....
            Map<String, Object> siteMenu = new HashMap<String, Object>();
            siteMenu.put("id", muid);
            List<SiteMenu> siteMenuList = siteMenuService.findPidAndPid(siteMenu);
            data.put("siteMenuList", siteMenuList);
            mav.addObject("data", data);
            mav.setViewName("/pages/WebSitePc/PublicInteraction/solicitList.jsp");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mav;
    }

    @Autowired
    private SolicitPublicService solicitPublicService;

    //查找 民意征集--分页
    @RequestMapping("/querySolicitPage")
    @ResponseBody
    public Map<String, Object> querySolicitPage(@RequestParam Map<String, Object> data,
                                                HttpServletRequest request, HttpServletResponse response) throws Exception {
        data.put("is_deleted", "0");
        data.put("visible", "1");
        Map<String, Object> result = new HashMap<String, Object>();
        PageInfo<SolicitPublic> page = solicitPublicService.listPage(data);
        result.put("total", page.getTotal());
        result.put("rows", page.getList());
        return result;
    }

    //公众互动--民意征集
    @RequestMapping(value = "/solicitView")
    @ResponseBody
    public ModelAndView solicitView(@RequestParam Map<String, Object> data,
                                    HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView();
        try {
            if (data.get("muid") != null) {
                String muid = data.get("muid").toString();
                //该 导航菜单 的上级 上级 ....
                Map<String, Object> siteMenu = new HashMap<String, Object>();
                siteMenu.put("id", muid);
                List<SiteMenu> siteMenuList = siteMenuService.findPidAndPid(siteMenu);
                data.put("siteMenuList", siteMenuList);
            } else {
                data.put("siteMenuList", null);
            }

            // 通过id 查找该 民意征集
            String id = new String(BaseCodec.decode(data.get("id").toString()));//解密-获取id
            data.put("id", id);
            List<SolicitPublic> solicitPublic = solicitPublicService.queryAll(data);
            if (solicitPublic != null) {
                data.put("solicitPublic", solicitPublic.get(0));
            }
            mav.addObject("data", data);
            mav.setViewName("/pages/WebSitePc/PublicInteraction/solicitView.jsp");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mav;
    }

    @Autowired
    private SolicitPublicCommentService solicitPublicCommentService;

    //查找 民意征集 评论 --分页
    @RequestMapping("/querySolicitCommentPage")
    @ResponseBody
    public Map<String, Object> querySolicitCommentPage(@RequestParam Map<String, Object> data,
                                                       HttpServletRequest request, HttpServletResponse response) throws Exception {
        data.put("is_deleted", "0");
        data.put("is_publish", "1");
        Map<String, Object> result = new HashMap<String, Object>();
        PageInfo<SolicitPublicComment> page = solicitPublicCommentService.listPage(data);
        result.put("total", page.getTotal());
        result.put("rows", page.getList());
        return result;
    }

    //点赞
    @RequestMapping(value = "/SolicitCommentAdd")
    @ResponseBody
    public Map<String, Object> SolicitCommentAdd(@RequestParam Map<String, Object> data, SolicitPublicComment solicitPublicComment,
                                                 HttpSession session, HttpServletRequest request,
                                                 HttpServletResponse response) throws Exception {
        try {
            // String fk_spid=data.get("fk_spid").toString();//获取 民意征集id
            String typeId = data.get("typeId").toString();//获取 民意征集id
            if (typeId.equals("0")) {
                String ip = getIpAddr(request);
                data.put("ip", ip);//查询该ip是否已经点赞过了
                data.put("is_deleted", "0");
                List<SolicitPublicComment> publicComment = solicitPublicCommentService.queryAll(data);
                if (publicComment != null && publicComment.size() > 0) {
                    data.put("success", false);
                    data.put("error", "抱歉，您已经点赞过了！");
                    return data;
                } else {
                    solicitPublicComment.setIp(ip);
                }
            }

            int exeCount = solicitPublicCommentService.add(solicitPublicComment);

            //查找目前的赞数目
            Map<String, Object> zandata = new HashMap<String, Object>();
            zandata.put("typeId", data.get("typeId").toString());
            zandata.put("fk_spid", data.get("fk_spid").toString());
            zandata.put("is_deleted", "0");
            List<SolicitPublicComment> zanNum = solicitPublicCommentService.queryAll(zandata);
            data.put("joinNum", zanNum.size());

            if (exeCount > 0) {
                data.put("success", true);
            } else {
                data.put("success", false);
            }

        } catch (Exception e) {
            data.put("success", false);
            data.put("error", ExceptionUtil.createStackTrackMessage(e));
            e.printStackTrace();
        }
        return data;
    }

    //网站地图
    @RequestMapping(value = "/siteMap")
    @ResponseBody
    public ModelAndView siteMapInterface(@RequestParam Map<String, Object> data,
                                         HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView();
        try {
            mav.addObject("data", data);
            mav.setViewName("/pages/WebSitePc/siteMap.jsp");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mav;
    }


    /**
     * 手机端wap网站 跨域访问
     *
     * @param data
     * @param request
     * @param response
     * @return
     * @throws Exception
     */

    //查找文章------手机端查找文章
    @RequestMapping(value = "/queryArticleAPP", produces = {"text/javascript;charset=UTF-8"})
    @ResponseBody
    public String queryArticleAPP(@RequestParam Map<String, Object> data,
                                  HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<ArticleList> articleList;
        data.put("is_deleted", "0");
        data.put("status", "2");//状态 草稿0,待审1,审核通过2,审核失败3
        data.put("articleSite", "'0','2'");//文章所属站点 0 外网,1内网，2内外网
        data.put("issue", "1");//是否网页显示
        // data.put("isHomePic", "1");//是否首页图片新闻
        //data.put("isRecommend", "0");//是否列表推荐 预留字段
        articleList = articleListService.queryListForIndex(data);
        //}
        String callback = data.get("callback").toString();

        String str = JSON.toJSONString(articleList);


        return callback + "(" + str + ")";
    }

    //查找文章--手机分页
    @RequestMapping(value = "/queryArticlePageAPP", produces = {"text/javascript;charset=UTF-8"})
    @ResponseBody
    public String queryArticlePagAPP(@RequestParam Map<String, Object> data,
                                     HttpServletRequest request, HttpServletResponse response) throws Exception {
        data.put("is_deleted", "0");
        data.put("status", "2");//状态 草稿0,待审1,审核通过2,审核失败3
        data.put("articleSite", "'0','2'");//文章所属站点 0 外网,1内网，2内外网
        data.put("issue", "1");//是否网页显示
        if (data != null && !data.get("categoryID").equals("")) {
            //查找子类
            Map<String, Object> dataCate = new HashMap<String, Object>();
            dataCate.put("id", data.get("categoryID"));
            List<ArticleCategory> categoryList = articleCategoryService.findChildAndChild(dataCate);
            if (categoryList != null && !categoryList.isEmpty()) {
                data.put("categoryID", categoryList.get(0).getName());
            }
        }
        Map<String, Object> result = new HashMap<String, Object>();
        PageInfo<ArticleList> page = articleListService.listPage(data);

        String callback = data.get("callback").toString();
        result.put("hasNextPage", page.isHasNextPage()); //是否还有下一页
        result.put("rows", page.getList());
        String str = JSON.toJSONString(result);

        return callback + "(" + str + ")";
    }

    //查找单独一篇文章--手机APP
    @RequestMapping(value = "/queryArticleByIdAPP", produces = {"text/javascript;charset=UTF-8"})
    @ResponseBody
    public String queryArticleByIdAPP(@RequestParam Map<String, Object> data, ArticleList entity,
                                      HttpServletRequest request, HttpServletResponse response) throws Exception {
        ArticleList art = new ArticleList();
        art.setId(Integer.parseInt(data.get("id").toString()));
        ArticleList articleList = articleListService.queryById(art);
        String callback = data.get("callback").toString();
        String str = JSON.toJSONString(articleList, SerializerFeature.WriteMapNullValue);
        return callback + "(" + str + ")";
    }

    //空气质量
    //查找24小时 城市小时 AQI数据----手机APP
    @RequestMapping(value = "/querylatestCityHourAQIAPP", produces = {"text/javascript;charset=UTF-8"})
    @ResponseBody
    public String QueryTAirCityHourAQIAPP(@RequestParam Map<String, Object> data,
                                          HttpServletRequest request, HttpServletResponse response) {
        //调用 信息发布
        //DbContextHolder.setDbType(DataSourceConst.THIRD); //调用service之前只需要设置上下文 调用相应的数据源
        List<Map<String, Object>> tDataList = new ArrayList<>();
        //DbContextHolder.clearDbType();
        String callback = data.get("callback").toString();
        String str = JSON.toJSONString(tDataList, SerializerFeature.WriteMapNullValue);
        return callback + "(" + str + ")";
    }


    // 获取ip
    private String getIpAddr(HttpServletRequest request) {
        String ipAddress = null;
        // ipAddress = this.getRequest().getRemoteAddr();
        ipAddress = request.getHeader("x-forwarded-for");
        if (ipAddress == null || ipAddress.length() == 0
                || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0
                || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0
                || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
            if (ipAddress.equals("127.0.0.1")) {
                // 根据网卡取本机配置的IP
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                ipAddress = inet.getHostAddress();
            }

        }

        // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
            // = 15
            if (ipAddress.indexOf(",") > 0) {
                ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
            }
        }
        return ipAddress;
    }


}
